AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include( 'shared.lua' )

local power = 90000000
local radius = 1500

function ENT:SpawnFunction( ply, tr )
	if ( !tr.Hit ) then return end
	local SpawnPos = tr.HitPos + tr.HitNormal * 16
	local ent = ents.Create("sent_spaceanon_hypermass")
	ent:SetPos( SpawnPos )
	ent:Spawn()
	ent:Activate()
	ent:SetColor(0,0,0,255)
	return ent
end

function ENT:Initialize()
	self.Entity:SetModel( "models/Combine_Helicopter/helicopter_bomb01.mdl" )
	self.Entity:PhysicsInit(SOLID_VPHYSICS)
	self.Entity:SetMoveType(MOVETYPE_VPHYSICS)
	self.Entity:SetSolid(SOLID_VPHYSICS)
	local phys = self.Entity:GetPhysicsObject()
	if (phys:IsValid()) then
		phys:EnableMotion(false)
	end
	--what kind of hypermass is it?
	if math.random(1,8) == 1 then --Was 1,50
		self.Wormhole = true
		self.Fixed = true
		local tmpmark = ents.Create("prop_physics")
		tmpmark:SetPos(Vector(0,0,0))
		tmpmark:SetModel("models/Combine_Helicopter/helicopter_bomb01.mdl")
		tmpmark:Spawn()
		while true do
			tmpmark:SetPos(Vector(math.random(-65530,65530),math.random(-65530,65530),math.random(-65530,65530)))
			if tmpmark:IsInWorld() then
				self.TelePos = tmpmark:GetPos()
				tmpmark:Remove()
				break
			end
		end
	else --only teleports sometimes
		self.Wormhole = false
	end
	self:ChangeVec()
end

function ENT:ChangeVec()
	if not self.Entity then return end
	self.vec = VectorRand()
	timer.Simple(3,self.ChangeVec, self)
end

function ENT:Think()
	local pos = self.Entity:GetPos()
	local ents = ents.FindInSphere(pos,radius)
	for _,v in pairs(ents) do
		if v:IsValid() then
			local range = v:GetPos():Distance(self.Entity:GetPos())
			local str = v:GetClass()
			if (not string.find(str, "physgun_beam")) and
				(not string.find(str, "predicted_viewmodel")) and
				(not (string.find(str, "func_") and (str ~= "func_physbox"))) and
				(not string.find(str, "info_")) and
				(not string.find(str, "point_")) and
				(not string.find(str, "path_")) and
				(not string.find(str, "node")) and
				(not string.find(str, "Environment")) and
				(not string.find(str, "environment")) and 	
				(not string.find(str, "sent_spaceanon")) and 
				(not string.find(str, "env_")) or string.find(str, "asteroid") then
				local dir = (v:GetPos()-self.Entity:GetPos()):Normalize()
				local phys = v:GetPhysicsObject()
				if math.random(1,15) == 1 then
					if (phys:IsValid()) then 
						constraint.RemoveAll(v)
						phys:EnableMotion(true)
						phys:Wake()
						if phys:GetMass() < 200 then
						phys:SetMass(200)
						end	
					end
				end
				if(phys:IsValid()) then
					local force = dir*-(power*phys:GetMass()/range+1^2)
					if v:GetMoveType() == 6 then
						phys:ApplyForceCenter(force)
					else
						v:SetVelocity(force/128)
					end
				end
			end
		end
	end
end

function ENT:Touch(ent)
	local str = ent:GetClass()
	if (not string.find(str, "physgun_beam")) and
		(not string.find(str, "predicted_viewmodel")) and
		(not (string.find(str, "func_") and (str ~= "func_physbox"))) and
		(not string.find(str, "info_")) and
		(not string.find(str, "point_")) and
		(not string.find(str, "path_")) and
		(not string.find(str, "node")) and
		(not string.find(str, "hypermass")) and
		(not string.find(str, "Environment")) and
		(not string.find(str, "environment")) and 	
		(not string.find(str, "sent_spaceanon")) and 
		(not string.find(str, "env_")) or string.find(str, "asteroid") or string.find(str, "asteroid") then
		if math.random(1,10) == 1 or self.Wormhole then
			constraint.RemoveAll(ent)
			while true do
				if self.Wormhole then --override
					ent:SetPos(self.TelePos)
					break
				end
				ent:SetPos(Vector(math.random(-65530,65530),math.random(-65530,65530),math.random(-65530,65530)))
				if ent:IsInWorld() then
					break
				end
			end
		else
			if ent:GetClass() == "player" then
				ent:Kill()
			else
				ent:Remove()
			end
		end
		local e = EffectData()
		e:SetOrigin(self.Entity:GetPos())
		e:SetNormal(self.vec)
		util.Effect("hypermass_refract_ring", e)
	end
end
